package com.edu.utils;

import com.alibaba.fastjson.JSON;
import com.edu.entity.FoodType;
import lombok.Data;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;

/**
 * @Author ZCN
 * @Date 2021/11/12 16:00
 * @Version 1.0   响应工具类
 **/
@Data
public class ResponseBody implements Serializable {
    private static final Integer SUCCESS = 0;
    private static final Integer FAILURE = -1;
    private Integer code; // 0：成功 -1：失败
    private String msg;
    private Long count;
    private List<Object> data;

    // 成功返回数据
    public static void success(HttpServletResponse resp,String msg,Long count,List<Object> data) throws IOException {
        ResponseBody success = new ResponseBody(ResponseBody.SUCCESS, msg, count, data);
        resp.getWriter().print(JSON.toJSONString(success));
    }
    // 失败返回数据
    public static void failure(HttpServletResponse resp,String msg){
        try {
            ResponseBody failure = new ResponseBody(ResponseBody.FAILURE, msg, 0L, null);
            resp.getWriter().print(JSON.toJSONString(failure));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public ResponseBody() {
    }

    public ResponseBody(Integer code, String msg, Long count, List<Object> data) {
        this.code = code;
        this.msg = msg;
        this.count = count;
        this.data = data;
    }

}
